package so.sao.code.service.service.impl;

import java.util.Date;
import java.util.UUID;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.sao.code.dto.request.CodeFileCreate;
import so.sao.code.service.domain.entity.CodeFileHandle;
import so.sao.code.service.repository.CodeFileHandleDao;
import so.sao.code.service.service.CodeFileHandleService;
import so.sao.ffw.dto.message.CodeFileHandleMessage;
import so.sao.ffw.feign.client.CodeFileHandleClient;
//import so.sao.ffw.feign.client.CodeFileHandleClient;
import so.sao.top.core.util.mapper.JsonMapper;

@Service
public class CodeFileHandleServiceImpl implements CodeFileHandleService {
	@Autowired
	private CodeFileHandleDao codeFileHandleDao;

	@Autowired
	private JsonMapper jsonMapper;

	@Autowired
	private CodeFileHandleClient codeFileHandleClient;

	@Transactional
	@Override
	public CodeFileHandle handleCodeFileParam(CodeFileCreate fileParam) {

		CodeFileHandle fileInfo = codeFileHandleDao.findByFilePathAndContainerNameAndFileType(fileParam.getFilePath(),
				fileParam.getContainerName(), fileParam.getFileType());
		if (fileInfo == null) {
			fileInfo = new CodeFileHandle();
			BeanUtils.copyProperties(fileParam, fileInfo);
			fileInfo.setCreateDate(new Date());
		} else {
			fileInfo.setUpdateDate(new Date());
		}
		fileInfo = codeFileHandleDao.save(fileInfo);
		CodeFileHandleMessage queueMessage = new CodeFileHandleMessage();
		BeanUtils.copyProperties(fileParam, queueMessage);
		queueMessage.setMessageId(UUID.randomUUID().toString());
		queueMessage.setCodeFileId(fileInfo.getId());
		// 发送参数到消息队列异步处理
		codeFileHandleClient.codeFileMessageToQueue(queueMessage);
		return fileInfo;
	}
}
